<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>拼音音频文件上传测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 600px;
            margin: 50px auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            color: #333;
            text-align: center;
            margin-bottom: 30px;
        }
        .form-group {
            margin-bottom: 20px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
            color: #555;
        }
        input[type="text"], input[type="file"] {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 16px;
            box-sizing: border-box;
        }
        button {
            background-color: #007bff;
            color: white;
            padding: 12px 30px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
            width: 100%;
        }
        button:hover {
            background-color: #0056b3;
        }
        button:disabled {
            background-color: #ccc;
            cursor: not-allowed;
        }
        .result {
            margin-top: 20px;
            padding: 15px;
            border-radius: 5px;
            display: none;
        }
        .success {
            background-color: #d4edda;
            border: 1px solid #c3e6cb;
            color: #155724;
        }
        .error {
            background-color: #f8d7da;
            border: 1px solid #f5c6cb;
            color: #721c24;
        }
        .progress {
            margin-top: 10px;
            display: none;
        }
        .progress-bar {
            width: 100%;
            height: 20px;
            background-color: #e9ecef;
            border-radius: 10px;
            overflow: hidden;
        }
        .progress-fill {
            height: 100%;
            background-color: #007bff;
            width: 0%;
            transition: width 0.3s ease;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>拼音音频文件上传测试</h1>
        
        <form id="uploadForm">
            <div class="form-group">
                <label for="pinyin">拼音:</label>
                <input type="text" id="pinyin" name="pinyin" placeholder="请输入拼音，如: ni3" required>
            </div>
            
            <div class="form-group">
                <label for="audioFile">音频文件:</label>
                <input type="file" id="audioFile" name="audio" accept="audio/*" required>
                <small style="color: #666;">支持格式: MP3, WAV, OGG, M4A (最大10MB)</small>
            </div>
            
            <button type="submit" id="submitBtn">上传文件</button>
        </form>
        
        <div class="progress" id="progress">
            <div class="progress-bar">
                <div class="progress-fill" id="progressFill"></div>
            </div>
            <div style="text-align: center; margin-top: 5px; font-size: 14px; color: #666;">
                上传中...
            </div>
        </div>
        
        <div class="result" id="result"></div>
    </div>

    <script>
        const API_BASE = 'http://localhost:7800/pinyin-api';
        
        document.getElementById('uploadForm').addEventListener('submit', async function(e) {
            e.preventDefault();
            
            const pinyin = document.getElementById('pinyin').value.trim();
            const audioFile = document.getElementById('audioFile').files[0];
            const submitBtn = document.getElementById('submitBtn');
            const result = document.getElementById('result');
            const progress = document.getElementById('progress');
            const progressFill = document.getElementById('progressFill');
            
            if (!pinyin) {
                showResult('请输入拼音', 'error');
                return;
            }
            
            if (!audioFile) {
                showResult('请选择音频文件', 'error');
                return;
            }
            
            // 检查文件大小
            if (audioFile.size > 10 * 1024 * 1024) {
                showResult('文件大小超过10MB限制', 'error');
                return;
            }
            
            // 检查文件类型
            const allowedTypes = ['audio/mpeg', 'audio/mp3'];
            if (!allowedTypes.includes(audioFile.type)) {
                showResult('不支持的文件格式，请上传音频文件', 'error');
                return;
            }
            
            // 显示进度条
            submitBtn.disabled = true;
            progress.style.display = 'block';
            result.style.display = 'none';
            
            try {
                const formData = new FormData();
                formData.append('pinyin', pinyin);
                formData.append('audio', audioFile);
                
                const response = await fetch(`${API_BASE}/upload-pinyin`, {
                    method: 'POST',
                    body: formData
                });
                
                const data = await response.json();
                
                if (data.success) {
                    const message = data.data.converted 
                        ? `文件上传并转换成功！\n拼音: ${data.data.pinyin}\n保存文件名: ${data.data.savedFileName}\n文件大小: ${(data.data.fileSize / 1024).toFixed(2)} KB`
                        : `文件上传成功！\n拼音: ${data.data.pinyin}\n保存文件名: ${data.data.savedFileName}\n文件大小: ${(data.data.fileSize / 1024).toFixed(2)} KB`;
                    
                    showResult(message, 'success');
                } else {
                    showResult(`上传失败: ${data.message}`, 'error');
                }
                
            } catch (error) {
                console.error('上传错误:', error);
                showResult(`上传错误: ${error.message}`, 'error');
            } finally {
                submitBtn.disabled = false;
                progress.style.display = 'none';
            }
        });
        
        function showResult(message, type) {
            const result = document.getElementById('result');
            result.textContent = message;
            result.className = `result ${type}`;
            result.style.display = 'block';
        }
        
        // 模拟上传进度
        function updateProgress(percent) {
            const progressFill = document.getElementById('progressFill');
            progressFill.style.width = percent + '%';
        }
    </script>
</body>
</html> 